<?php
class OrderComponentController extends RestrictedBaseController {
	
	public function getView($id) {
		$this->layout = null;
		
		$orderComponent = OrderComponent::with(array('order', 'component'))->find($id);
		$ccDates = OrderComponent::getCostsCentresWithProbableDates($id);
		
		return View::make('ordercomponent.view')->with('orderComponent', $orderComponent)
			->with('ccDates', $ccDates)
			->with('hasCurrentCostsCentrePrivilege', $orderComponent->hasCurrentCostsCentrePrivilege());
	}
	
	public function postLate() {
		$this->layout = null;
		$result = OrderComponent::getLateComponents($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postOnSchedule() {
		$this->layout = null;
		$result = OrderComponent::getOnScheduleComponents($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postAheadSchedule() {
		$this->layout = null;
		$result = OrderComponent::getAheadScheduleComponents($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postLateOrders() {
		$this->layout = null;
		$result = OrderComponent::getLateOrders($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postOnScheduleOrders() {
		$this->layout = null;
		$result = OrderComponent::getOnScheduleOrders($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postAheadScheduleOrders() {
		$this->layout = null;
		$result = OrderComponent::getAheadScheduleOrders($this->assembleFilterOptions(), Input::get('iDisplayStart'), Input::get('iDisplayLength'));
	
		return Response::json(array(
			"sEcho" => intval(Input::get('sEcho')),
			"iTotalRecords" => $result['total'],
			"iTotalDisplayRecords" => $result['total'],
			"aaData" => $result['data']
		));
	}
	
	public function postChangeStep() {
		$this->layout = null;
		$orderComponent = OrderComponent::find(Input::get('id'));
		
		if ($orderComponent->hasCurrentCostsCentrePrivilege()) {
			if ($orderComponent->current_step != Input::get('step')) { 
				$orderComponent->updateStep(Input::get('step'));
				
				return Response::json(array(
					'success' => true
				));
			}
			
			return Response::json(array(
				'success' => false,
				'message' => 'Selecione o centro de custo para o qual o item será movido.'
			));
		}
		
		return Response::json(array(
			'success' => false,
			'message' => 'Você não possui permissão para alterar o centro de custo atual'
		));
	}
	
	public function postAdvanceStep() {
		$this->layout = null;
		$orderComponent = OrderComponent::find(Input::get('id'));
	
		if ($orderComponent->hasCurrentCostsCentrePrivilege()) {
			$orderComponent->advanceStep();
	
			return Response::json(array(
				'success' => true
			));
		}
	
		return Response::json(array(
			'success' => false,
			'message' => 'Você não possui permissão para alterar o centro de custo atual'
		));
	}
	
	public function postMarkComplete() {
		$this->layout = null;
		$orderComponent = OrderComponent::find(Input::get('id'));
	
		if ($orderComponent->hasCurrentCostsCentrePrivilege()) {
			$orderComponent->markComplete();
		
			return Response::json(array(
				'success' => true
			));
		}
		
		return Response::json(array(
			'success' => false,
			'message' => 'Você não possui permissão para alterar o centro de custo atual'
		));
	}
	
	private function assembleFilterOptions() {
		$options = array();
		$options['refDate'] = Input::get('refDate');
		$options['orderNumber'] = Input::get('orderNumber');
		$options['customerId'] = Input::get('customerId');
		$options['costsCentresId'] = Input::get('costsCentresId');
		$options['productionNumber'] = Input::get('productionNumber');
		$options['accessory'] = Input::get('accessory');
		$options['code'] = Input::get('code');
		
		return $options;
	}
	
}